package com.gali.leetcode.day3;

import com.sun.deploy.util.ArrayUtil;

import java.util.Arrays;

/**
 * @Auther: gali
 * @Date: 2022/7/16-07-16-20:30
 * @Description:寻找两个正序数组的中位数
 */
public class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int []nums;
        int l1=nums1.length;
        int l2=nums2.length;
        nums=new int[l1+l2];
        //第一个数组为空
        if (l1==0){
            if (l2%2==0){
                return (nums2[l2/2]+nums2[l2/2-1])/2.0;
            }else {
                return nums2[l2/2];
            }
        }
        //第二个数组为空
        if (l2==0){
            if (l1%2==0){
                return (nums1[l1/2]+nums1[l1/2-1])/2.0;
            }else {
                return nums1[l1/2];
            }
        }
        //合并数组
        int l3=nums.length;
        for (int i = 0; i <l1 ; i++) {
            nums[i]=nums1[i];
        }
        for (int i = 0; i <l2 ; i++) {
            nums[i+l1]=nums2[i];
        }
        Arrays.sort(nums);
        if (l3%2==0){
            return (nums[l3/2]+nums[l3/2-1])/2.0;
        }else {
            return nums[l3/2];
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int []arr1={1,3};
        int []arr2={2};
        solution.findMedianSortedArrays(arr1,arr2);
    }
}
